from fastapi import APIRouter, Cookie, status, HTTPException

from typing import Annotated, Optional

from src.region_service.services.region import Region_service
from src.region_service.schemas import (
    Upload_region_form
)

region_router = APIRouter(prefix="/region", tags=["region"])


@region_router.post(
    "",
    # response_model=Region_schema,
    status_code=status.HTTP_201_CREATED
)
async def create_region(
    payload: Upload_region_form,
    id: Annotated[int | None, Cookie()] = None
):
    return await Region_service().create_region(
        payload,
        user_id=id
    )


@region_router.get(
    "/{regionId}",
    # response_model=Region_schema_with_accountId,
    status_code=status.HTTP_200_OK
)
async def get_region(
    regionId: Optional[int],
    id: Annotated[int | None, Cookie()] = None
):
    if regionId is None or regionId <= 0:
        raise HTTPException(status_code=400, detail="bad data")

    return await Region_service().get_region(
        regionId,
        user_id=id
    )


@region_router.put(
    "/{regionId}",
    # response_model=Region_schema,
    status_code=status.HTTP_200_OK
)
async def edit_region(
    regionId: int,
    payload: Upload_region_form,
    id: Annotated[int | None, Cookie()] = None
):
    if regionId is None:
        raise HTTPException(status_code=400, detail="bad data")

    return await Region_service().edit_region(
        regionId,
        payload,
        user_id=id
    )


@region_router.delete(
    "/{regionId}",
    status_code=status.HTTP_200_OK
)
async def delete_regionType(
    regionId: int,
    id: Annotated[int | None, Cookie()] = None
):
    if regionId is None or regionId <= 0:
        raise HTTPException(status_code=400, detail="bad data")

    return await Region_service().delete_region(
        regionId,
        user_id=id
    )
